package com.itboye.ihomebank.activity.finance.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.HaveInhaAdapter;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.base.ptr.BasePtr;
import com.itboye.ihomebank.bean.HaveInBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.finance.MyTouZiActivity;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.util.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
/**
 * 我的投资(已完成)
 */
@SuppressLint("ValidFragment")
public class YijingHuiKuanFrament extends BaseFragment implements Observer ,HaveInhaAdapter.ClickListener{
    private int type;
    private ListView listView;
    private HaveInhaAdapter adapter;
    private HaveInBean list;
    List<HaveInBean.InfoList> lists;
    private JinRongPresenter presenter;
    String uid;
    int pageSize = 10;
    int pageNum = 1;

    PtrFrameLayout commend_anchor_ptr;//刷新
    private LinearLayout linNull, linhuikuan;

    @Override
    public int initView() {
        return R.layout.fragment_haveinhand;
    }

    public YijingHuiKuanFrament(int type) {
        this.type = type;
    }

    @Override
    public void onMyActivityCreated() {
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        list = new HaveInBean();
        lists=new ArrayList<HaveInBean.InfoList>();

        presenter = new JinRongPresenter(this);
        uid = (String) SPUtils.get(getActivity(), null, SPContants.USER_ID, "");
        showProgressDialog("数据加载中...", true);
        presenter.onMyTouZhi(uid, pageNum + "", pageSize + "", type + "");
        adapter = new HaveInhaAdapter(lists, getActivity(),this);
        listView.setAdapter(adapter);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageNum++;
                presenter.onMyTouZhi(uid, pageNum + "", pageSize + "", type + "");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.onMyTouZhi(uid, pageNum + "", pageSize + "", type + "");
            }
        });
        commend_anchor_ptr.autoRefresh();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent Intet = new Intent(getActivity(), MyTouZiActivity.class);
                Intet.putExtra("id", lists.get(position).getId());
                startActivity(Intet);
            }
        });

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);

        if (entity != null) {
            if (!entity.getCode().equals(0)) {
                if (entity.getEventType() == JinRongPresenter.my_tou_zi_success) {
                    if (pageNum == 1) {
                        lists.clear();
                    }
                    commend_anchor_ptr.refreshComplete();//设置刷新完成
                    list = (HaveInBean) entity.getData();
                    lists.addAll(list.getInfo());
                    if (lists.size() > 0) {
                        linhuikuan.setVisibility(View.GONE);
                        commend_anchor_ptr.setVisibility(View.VISIBLE);
                        adapter.notifyDataSetChanged();
                    }else{
                        linhuikuan.setVisibility(View.VISIBLE);
                        commend_anchor_ptr.setVisibility(View.GONE);
                    }
                }
            }
            try {
                closeProgressDialog();
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void onCle(View v, Context context) {

    }
}